﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_category : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadAllCategory();
        }
        if (Session["adminlogin"] == null)
        {
            Response.Redirect("adminlogin.aspx");
        }
    }
    protected void LoadAllCategory()
    {
        CategoryBus CategoryBus = new CategoryBus();
        grvCategory.DataSource = CategoryBus.GetAllCategory();
        grvCategory.DataBind();
    }

    protected void grvCategory_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvCategory.EditIndex = e.NewEditIndex;
        LoadAllCategory();
    }

    protected void grvCategory_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvCategory.EditIndex = -1;
        LoadAllCategory();
    }

    protected void grvCategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //lay vi tri row
        
        GridViewRow row = (GridViewRow)grvCategory.Rows[e.RowIndex];
        Category objCategory = new Category();
        //set gia tri cho doi tuong
        int catID = Convert.ToInt32(grvCategory.DataKeys[e.RowIndex].Value.ToString());
        objCategory.CatID = catID;
        TextBox txtCatName = (TextBox)row.FindControl("txtCatName");
        objCategory.CatName = txtCatName.Text;
        TextBox txtCatDescription = (TextBox)row.FindControl("txtCatDescription");
        objCategory.CatDescription = txtCatDescription.Text;
        
        //
        CategoryBus CategoryBus = new CategoryBus();
        CategoryBus.UpdateCategory(objCategory);
        grvCategory.EditIndex = -1;
        LoadAllCategory();

    }
    protected void grvCategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //lay vi ID theo vi tri qua datakeys
        int catID = Convert.ToInt32(grvCategory.DataKeys[e.RowIndex].Value);

        CategoryBus CategoryBus = new CategoryBus();
        //neu category co chua san pham thi se khong xoa duoc
        if (!CategoryBus.DeleteCategory(catID))
            Response.Write("<script>alert('This category contains items')</script>");
        else
            //xoa xong load len
            LoadAllCategory();
    }
    protected void btnAddcategory_Click(object sender, EventArgs e)
    {
        CategoryBus CategoryBus = new CategoryBus();
        Category objCategory = new Category();
        objCategory.CatName = txtNameCategory.Text;
        objCategory.CatDescription = txtDescriptionCategory.Text;
        if (CategoryBus.AddCategory(objCategory))
        {
            Response.Write("<script>alert('Add new category successfully')</script>");
            //them xong thi load len
            LoadAllCategory();
            //xoa textbox
            resetTextbox();
        }
        else
        {
            Response.Write("<script>alert('Add new category fail!')</script>");
        }
    }
    public void resetTextbox()
    {
        txtNameCategory.Text = "";
        txtDescriptionCategory.Text = "";
    }
    protected void grvCategory_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
